#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import sys

import pygame
from pygame.constants import *

from TextBox import TextBox, PassWord_textbox
from constants.ColorConstants import COLOR_BLACK, COLOR_GREEN, COLOR_WHILE
from constants.SystemConstants import *
from entity import hero_plane
from source import *
from utils import LoginRegister
from utils.LoginRegister import lgoin_check
from utils.SoundControlUtils import SoundControlUtils


class ChangePage():
    # 排名界面
    def paint_rank():
        SoundControlUtils.sound_bgm_rank.play()
        screen.blit(bg_rank, (0, 0))
        back_rect.left, back_rect.top = (width - back_rect.width) / 2, 900
        f = open('data/rank.txt')
        # conn = database.connn()
        # cursor = conn.cursor(as_dict=True)
        # cursor.execute('SELECT * FROM p_score order by score desc  ')
        i = 0
        rank_font = pygame.font.Font("font/方正舒体__GBK.ttf", 40)
        title1 = rank_font.render("排名", True, COLOR_BLACK)
        title2 = rank_font.render("昵称", True, COLOR_BLACK)
        title3 = rank_font.render("分数", True, COLOR_BLACK)
        t1 = 20
        t2 = 220
        t3 = 420
        screen.blit(title1, (t1, 120))
        screen.blit(title2, (t2, 120))
        screen.blit(title3, (t3, 120))
        for i in range(1, 11):
            user_id = f.readline()[:-1]
            score_str = f.readline()[:-1]
            score = int(score_str, 10)
            rank1 = rank_font.render("第%d名" % i, True, COLOR_BLACK)
            rank2 = rank_font.render(user_id, True, COLOR_BLACK)
            rank3 = rank_font.render(str(score), True, COLOR_BLACK)
            screen.blit(rank1, (t1, 65 * (i + 2)))
            screen.blit(rank2, (t2, 65 * (i + 2)))
            screen.blit(rank3, (t3, 65 * (i + 2)))
            if i == 10:
                break
        screen.blit(back_image, back_rect)
        pygame.display.update()
        f.close()
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    return EXIT_PAGE
                else:
                    if pygame.mouse.get_pressed()[0]:
                        pos = pygame.mouse.get_pos()
                        if back_rect.left < pos[0] < back_rect.right and back_rect.top < pos[1] < back_rect.bottom:
                            SoundControlUtils.sound_bgm_rank.stop()
                            SoundControlUtils.sound_bgm1.play()
                            return MAIN_PAGE

    # 显示操作说明的界面
    def paint_opreat():
        screen.blit(show_caozuo_back, (0, 0))
        back_rect.left, back_rect.top = (width - back_rect.width) / 2, 900
        screen.blit(back_image, back_rect)
        pygame.display.update()
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                else:
                    if pygame.mouse.get_pressed()[0]:
                        pos = pygame.mouse.get_pos()
                        if back_rect.left < pos[0] < back_rect.right and back_rect.top < pos[1] < back_rect.bottom:
                            return MAIN_PAGE

    # 结算打印界面
    def end_paint(user_id, score):
        ChangePage.write_score(user_id, score)
        SoundControlUtils.sound_bgm_putong.stop()
        SoundControlUtils.sound_bgm_level2.stop()
        SoundControlUtils.sound_lastone.stop()
        SoundControlUtils.sound_bgm1.play(-1)
        latest_score = score_font.render("你的得分： %d" % score, True, COLOR_WHILE)
        again_but_rect.left = (bg_size[0] - again_but_rect.width) // 2
        again_but_rect.top = 700
        back_but_rect.left = (bg_size[0] - back_but_rect.width) // 2
        back_but_rect.top = again_but_rect.top + 50
        clock = pygame.time.Clock()

        while True:
            screen.blit(bg_end, (0, 0))
            screen.blit(latest_score, ((bg_size[0] - latest_score.get_rect().width) // 2, 300))
            screen.blit(again_but, again_but_rect)
            screen.blit(back_but, back_but_rect)
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == QUIT:
                    return EXIT_PAGE
                if pygame.mouse.get_pressed()[0]:
                    pos = pygame.mouse.get_pos()
                    if back_but_rect.left < pos[0] < back_but_rect.right and \
                            back_but_rect.top < pos[1] < back_but_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        # sound_bgm1.play(-1)
                        return MAIN_PAGE  # 0为返回主页
                    if again_but_rect.left < pos[0] < again_but_rect.right and \
                            again_but_rect.top < pos[1] < again_but_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        # sound_bgm_putong.play(-1)
                        return CHOOSE_PLANE_PAGE  # 1为再来一次
            clock.tick(60)

    # 选择战机界面
    def paint_choose_plane():
        plane = []
        title_form = pygame.font.Font("font/方正舒体__GBK.ttf", 40)
        title = title_form.render("选择你的战机!!", True, COLOR_BLACK)
        plane.append(hero_plane.start1_plane(bg_size=bg_size))
        plane.append(hero_plane.start2_plane(bg_size=bg_size))
        plane.append(hero_plane.start3_plane(bg_size=bg_size))
        screen.blit(bg_choic, (0, -3000))
        screen.blit(title, (160, 200))
        interval = 70

        plane[0].rect.left = 35;
        plane[1].rect.left = plane[0].rect.right + interval
        plane[2].rect.left = plane[1].rect.right + interval
        plane[0].rect.top = plane[1].rect.top = 700
        plane[2].rect.top = 725
        clock = pygame.time.Clock()
        turn_picture = True
        time = 0
        choice = 1
        i = 0
        j = 1
        flag = False
        while True:
            #背景控制
            i += 2
            if i == 2000:
                i = 0

            #事件判断
            for event in pygame.event.get():
                #退出
                if event.type == QUIT:
                    return EXIT_PAGE
                if event.type == KEYDOWN:
                    if event.key == K_LEFT:
                        SoundControlUtils.sound_button.play()
                        if choice > 0:
                            choice = (choice - 1) % 3
                        else:
                            choice = 2
                    if event.key == K_RIGHT:
                        SoundControlUtils.sound_button.play()
                        choice = (choice + 1) % 3
                    if event.key == K_RETURN:
                        SoundControlUtils.choose_plane_fly.play()
                        flag = True
            if not (time % 11):
                turn_picture = not turn_picture

            # 根据是否选择战机确定画面
            if not flag:
                screen.blit(bg_choic, (0, -2000 + i))
                screen.blit(title, (160, 200))
                for me in plane:
                    if turn_picture:
                        screen.blit(me.fly_image1, me.rect)
                    else:
                        screen.blit(me.fly_image2, me.rect)
                space1 = 20
                space2 = space1 / 2
                space3 = 25
                if choice == 0:
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[0].rect.left - space3, plane[1].rect.top - space2,),
                                     (plane[0].rect.left - space3, plane[1].rect.bottom + space2),
                                     4)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[0].rect.right + space3, plane[1].rect.top - space2,),
                                     (plane[0].rect.right + space3, plane[1].rect.bottom + space2),
                                     4)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[0].rect.right + space3, plane[1].rect.top - space2,),
                                     (plane[0].rect.left - space3, plane[1].rect.top - space2),
                                     4)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[0].rect.right + space3, plane[1].rect.bottom + space2,),
                                     (plane[0].rect.left - space3, plane[1].rect.bottom + space2),
                                     4)
                elif choice == 1:
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[1].rect.left - space1, plane[1].rect.top - space2,),
                                     (plane[1].rect.left - space1, plane[1].rect.bottom + space2),
                                     4)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[1].rect.right + space1, plane[1].rect.top - space2,),
                                     (plane[1].rect.right + space1, plane[1].rect.bottom + space2),
                                     4)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[1].rect.right + space1, plane[1].rect.top - space2,),
                                     (plane[1].rect.left - space1, plane[1].rect.top - space2),
                                     4)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[1].rect.right + space1, plane[1].rect.bottom + space2,),
                                     (plane[1].rect.left - space1, plane[1].rect.bottom + space2),
                                     4)
                else:
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[2].rect.left - space1, plane[1].rect.top - space2,),
                                     (plane[2].rect.left - space1, plane[1].rect.bottom + space2),
                                     4)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[2].rect.right + space1, plane[1].rect.top - space2,),
                                     (plane[2].rect.right + space1, plane[1].rect.bottom + space2),
                                     4)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[2].rect.right + space1, plane[1].rect.top - space2,),
                                     (plane[2].rect.left - space1, plane[1].rect.top - space2),
                                     4)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (plane[2].rect.right + space1, plane[1].rect.bottom + space2,),
                                     (plane[2].rect.left - space1, plane[1].rect.bottom + space2),
                                     4)
            else:
                screen.blit(bg_choic, (0, -2000 + i))
                plane[choice].rect.top = plane[choice].rect.top - j
                j += 0.2
                if turn_picture:
                    screen.blit(plane[choice].fly_image1, (plane[choice].rect.left, plane[choice].rect.top))
                else:
                    screen.blit(plane[choice].fly_image2, (plane[choice].rect.left, plane[choice].rect.top))
                if plane[choice].rect.bottom < 0:
                    StaticConstans.PLANE_NUMBER = choice
                    return START_PAGE
            time = (time + 1) % 60
            pygame.display.flip()
            clock.tick(60)

    # 登录界面
    def paint_login(is_login):
        # 判断是否输入用户名
        # user = User_inform.User()
        user_id = ""
        is_user_id = True
        show_login_fail = False
        screen.blit(background_login, (0, 0))
        user_id_text = login_font.render("用户名 :", False, (0, 0, 0))
        passward_text = login_font.render("密码 :", False, (0, 0, 0))
        login_fail_text = login_fail_font.render("Error！！！ Please Input Again", False, COLOR_BLACK)
        login_turn_rect.left = (bg_size[0] - login_turn_rect.width) // 2
        login_turn_rect.top = 650
        reg_turn_rect.left = (bg_size[0] - reg_turn_rect.width) // 2
        reg_turn_rect.top = login_turn_rect.top
        button_end_rect.left = (width - register_but_rect.width) / 2
        button_end_rect.top = reg_turn_rect.top + 50
        #
        login_but_rect2.left = (bg_size[0] - login_but_rect2.width) // 2
        login_but_rect2.top = login_turn_rect.top - 50
        register_but_rect2.left = (bg_size[0] - register_but_rect2.width) // 2
        register_but_rect2.top = login_turn_rect.top - 50

        LOGIN_FAIL = pygame.USEREVENT + 6
        login_fail_event = pygame.event.Event(LOGIN_FAIL)

        # 创建文本框
        user_id_text_box = TextBox(200, 30, 200, 450)
        pswd_text_box = PassWord_textbox(200, 30, 200, 500)

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return EXIT_PAGE
                elif event.type == pygame.KEYDOWN:
                    # Tab键转换用户名密码的输入
                    if event.key == K_TAB:
                        is_user_id = not is_user_id
                    # 用户名输入
                    elif is_user_id:
                        user_id_text_box.key_down(event)
                    # 用户密码输入
                    else:
                        pswd_text_box.key_down(event)

                elif event.type == LOGIN_FAIL:
                    show_login_fail = False
                elif event.type == MOUSEBUTTONDOWN:
                    pos = event.pos

                    # 切换登录和注册界面
                    if login_turn_rect.left < pos[0] < login_turn_rect.right and \
                            login_turn_rect.top < pos[1] < login_turn_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        pswd_text_box.text = pswd_text_box.show_text = ""
                        user_id_text_box.text = user_id_text_box.show_text = ""
                        # 用户登录和注册界面鉴别条件转换
                        if is_login:
                            return REGISTER_PAGE
                        else:
                            return LOGIN_PAGE

                    # 登录
                    elif login_but_rect2.left < pos[0] < login_but_rect2.right and \
                            login_but_rect2.top < pos[1] < login_but_rect2.bottom:
                        SoundControlUtils.sound_button.play()
                        if is_login and LoginRegister.lgoin_check(user_id_text_box.text, pswd_text_box.text):
                            USER_ID = user_id_text_box.text
                            return MAIN_PAGE
                        else:
                            pygame.time.set_timer(LOGIN_FAIL, 2 * 1000)
                            print("登录失败")
                            is_user_id = True
                            user_id_text_box.text = user_id_text_box.show_text = ""
                            pswd_text_box.text = pswd_text_box.show_text = ""
                            show_login_fail = True
                    elif button_end_rect.left < event.pos[0] < button_end_rect.right and \
                            button_end_rect.top < event.pos[1] < button_end_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        # pygame.quit()
                        # sys.exit()
                        return EXIT_PAGE

            pygame.time.delay(33)

            screen.blit(background_login, (0, 0))
            user_id_text_box.draw(screen)
            pswd_text_box.draw(screen)

            screen.blit(user_id_text, (50, 450))
            screen.blit(passward_text, (50, 500))
            screen.blit(button_end, button_end_rect)
            if show_login_fail:
                screen.blit(login_fail_text, (170, 550))

            screen.blit(login_turn_imame, login_turn_rect)
            screen.blit(login_but_image2, login_but_rect2)

            pygame.display.flip()

    #注册界面
    def paint_register_page(is_login):
        is_user_id = True
        show_login_fail = False
        screen.blit(background_login, (0, 0))
        user_id_text = login_font.render("用户名 :", False, (0, 0, 0))
        passward_text = login_font.render("密码 :", False, (0, 0, 0))
        login_fail_text = login_fail_font.render("Error！！！ Please Input Again", False, COLOR_BLACK)
        login_turn_rect.left = (bg_size[0] - login_turn_rect.width) // 2
        login_turn_rect.top = 650
        reg_turn_rect.left = (bg_size[0] - reg_turn_rect.width) // 2
        reg_turn_rect.top = login_turn_rect.top
        button_end_rect.left = (width - register_but_rect.width) / 2
        button_end_rect.top = reg_turn_rect.top + 50
        #
        login_but_rect2.left = (bg_size[0] - login_but_rect2.width) // 2
        login_but_rect2.top = login_turn_rect.top - 50
        register_but_rect2.left = (bg_size[0] - register_but_rect2.width) // 2
        register_but_rect2.top = login_turn_rect.top - 50

        LOGIN_FAIL = pygame.USEREVENT + 6
        login_fail_event = pygame.event.Event(LOGIN_FAIL)

        # 创建文本框
        user_id_text_box = TextBox(200, 30, 200, 450)
        pswd_text_box = PassWord_textbox(200, 30, 200, 500)

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return EXIT_PAGE
                elif event.type == pygame.KEYDOWN:
                    # Tab键转换用户名密码的输入
                    if event.key == K_TAB:
                        is_user_id = not is_user_id
                    # 用户名输入
                    elif is_user_id:
                        user_id_text_box.key_down(event)
                    # 用户密码输入
                    else:
                        pswd_text_box.key_down(event)

                elif event.type == LOGIN_FAIL:
                    show_login_fail = False
                elif event.type == MOUSEBUTTONDOWN:
                    pos = event.pos

                    # 切换登录和注册界面
                    if login_turn_rect.left < pos[0] < login_turn_rect.right and \
                            login_turn_rect.top < pos[1] < login_turn_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        pswd_text_box.text = pswd_text_box.show_text = ""
                        user_id_text_box.text = user_id_text_box.show_text = ""
                        # 用户登录和注册界面鉴别条件转换
                        if is_login:
                            return REGISTER_PAGE
                        else:
                            return LOGIN_PAGE
                    elif login_but_rect2.left < pos[0] < login_but_rect2.right and \
                            login_but_rect2.top < pos[1] < login_but_rect2.bottom:
                        SoundControlUtils.sound_button.play()
                        if not is_login and LoginRegister.register(user_id_text_box.text, pswd_text_box.text):
                            USER_ID = user_id_text_box.text
                            print(USER_ID)
                            return MAIN_PAGE
                    elif button_end_rect.left < event.pos[0] < button_end_rect.right and \
                            button_end_rect.top < event.pos[1] < button_end_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        # pygame.quit()
                        # sys.exit()
                        return EXIT_PAGE
            # 绘制注册按钮


            pygame.time.delay(33)

            screen.blit(background_login, (0, 0))
            user_id_text_box.draw(screen)
            pswd_text_box.draw(screen)

            screen.blit(user_id_text, (50, 450))
            screen.blit(passward_text, (50, 500))
            screen.blit(button_end, button_end_rect)

            screen.blit(reg_turn_iamge, reg_turn_rect)
            screen.blit(register_but_image2, register_but_rect2)
            # screen.blit(login_turn_imame, login_turn_rect)
            # screen.blit(login_but_image2, login_but_rect2)

            pygame.display.flip()

    # 绘制  原始界面
    def paint_reg_login():
        # 背景音乐起
        SoundControlUtils.sound_bgm1.play(-1);

        # 按钮位置设置

        login_but_rect.left = (width - start_but_rect.width) / 2
        login_but_rect.top = 500
        register_but_rect.left = (width - register_but_rect.width) / 2
        register_but_rect.top = login_but_rect.top + 50
        button_end_rect.left = (width - register_but_rect.width) / 2
        button_end_rect.top = register_but_rect.top + 50

        screen.blit(background_login, (0, 0))
        screen.blit(login_but_image, login_but_rect)
        screen.blit(register_but_image, register_but_rect)
        screen.blit(button_end, button_end_rect)
        pygame.display.update()
        clock = pygame.time.Clock()

    # 行为判断  原始页面
    def reg_login_action():
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    return EXIT_PAGE
                if event.type == MOUSEBUTTONDOWN:
                    if login_but_rect.left < event.pos[0] < login_but_rect.right and \
                            login_but_rect.top < event.pos[1] < login_but_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        # ChangePage.main(ChangePage.login(True))
                        return LOGIN_PAGE
                    if register_but_rect.left < event.pos[0] < register_but_rect.right and \
                            register_but_rect.top < event.pos[1] < register_but_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        # ChangePage.main(ChangePage.login(False))
                        return REGISTER_PAGE
                    if button_end_rect.left < event.pos[0] < button_end_rect.right and \
                            button_end_rect.top < event.pos[1] < button_end_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        # pygame.quit()
                        # sys.exit()
                        return EXIT_PAGE
                # clock.tick(60)

    # 原始界面
    def reg_login2():
        login_but_rect.left = (width - start_but_rect.width) / 2
        login_but_rect.top = 500
        register_but_rect.left = (width - register_but_rect.width) / 2
        register_but_rect.top = login_but_rect.top + 50
        button_end_rect.left = (width - register_but_rect.width) / 2
        button_end_rect.top = register_but_rect.top + 50
        screen.blit(background_login, (0, 0))
        screen.blit(login_but_image, login_but_rect)
        screen.blit(register_but_image, register_but_rect)
        screen.blit(button_end, button_end_rect)
        pygame.display.update()
        clock = pygame.time.Clock()
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == MOUSEBUTTONDOWN:
                    if login_but_rect.left < event.pos[0] < login_but_rect.right and \
                            login_but_rect.top < event.pos[1] < login_but_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        return login(True)
                    if register_but_rect.left < event.pos[0] < register_but_rect.right and \
                            register_but_rect.top < event.pos[1] < register_but_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        return login(False)
                    if button_end_rect.left < event.pos[0] < button_end_rect.right and \
                            button_end_rect.top < event.pos[1] < button_end_rect.bottom:
                        SoundControlUtils.sound_button.play()
                        pygame.quit()
                        sys.exit()
                clock.tick(60)

    # 主界面
    def paint_main_page():
        title_form = pygame.font.Font("font/方正舒体__GBK.ttf", 40)
        start_but_rect.left = (width - start_but_rect.width) / 2
        start_but_rect.top = 450
        rank_but_rect.left = (width - rank_but_rect.width) / 2
        rank_but_rect.top = start_but_rect.top + 50
        volume1_button_rect.left = (width - volume1_button_rect.width) / 2
        volume1_button_rect.top = rank_but_rect.top + 50
        show_way_rect.left = (width - volume1_button_rect.width) / 2
        show_way_rect.top = volume1_button_rect.top + 50
        logout_but_rect.left = (width - logout_but_rect.width) / 2
        logout_but_rect.top = show_way_rect.top + 50
        button_end_rect.left = (width - register_but_rect.width) / 2
        button_end_rect.top = logout_but_rect.top + 50

        choice = 0
        clock = pygame.time.Clock()
        paint_set = False
        H_j = 20
        H_y = -9
        H_i = (height - image_bg_set1_rect.bottom + image_bg_set1_rect.top) / 2 + image_bg_set1_rect.bottom
        volume_bg_pos = 220
        volume_effect_pos = 220
        button_volume1_rect.left = volume_bg_pos
        button_volume2_rect.left = volume_effect_pos


        while True:
            button_end_rect.left = (width - register_but_rect.width) / 2
            button_end_rect.top = logout_but_rect.top + 50
            if choice == 1:
                # score = start(choose_plane())
                return START_PAGE
                # if score >= 0:
                #     choice = end_paint(user_id, score)
                #     button_volume1_rect.left, button_volume1_rect.top = volume_bg_pos, 339
                #     button_volume2_rect.left, button_volume2_rect.top = volume_effect_pos, 439
                #     sound_bgm_die.stop()
                #     sound_bgm1.play(-1)
                # else:
                #     choice = 0
            else:
                if paint_set:
                    if H_j > 0:
                        if H_i > 0:
                            H_j -= 0.2
                            H_i -= H_j

                    # 设置音量界面背景和关闭按钮位置
                    image_bg_set1_rect.left = (width - image_bg_set1_rect.width) / 2
                    image_bg_set1_rect.top = (height - image_bg_set1_rect.bottom + image_bg_set1_rect.top) / 2 + H_i
                    image_bg_set2_rect.right = image_bg_set1_rect.right + 17
                    image_bg_set2_rect.top = image_bg_set1_rect.top - 17
                    # 设置音量滑动按钮位置
                    button_volume1_rect.top = image_bg_set1_rect.top + 138
                    button_volume2_rect.top = image_bg_set1_rect.top + 237
                    # button_volume1_rect.left = volume_bg_pos
                    # button_volume2_rect.left = volume_effect_pos

                    v_bg = font_volume.render("音量", True, COLOR_BLACK)
                    v_ef = font_volume.render("音效", True, COLOR_BLACK)
                    v_set = font_volume.render("设置", True, COLOR_BLACK)
                    line_left, line_right = image_bg_set1_rect.left + 150, image_bg_set1_rect.left + 400

                    # 绘制背景和两个框
                    screen.blit(background_login2, (0, 0))
                    screen.blit(image_bg_set1, image_bg_set1_rect)
                    screen.blit(image_bg_set2, image_bg_set2_rect)
                    # 绘制三个词
                    screen.blit(v_bg, (image_bg_set1_rect.left + 50, image_bg_set1_rect.top + 130))
                    screen.blit(v_ef, (image_bg_set1_rect.left + 50, image_bg_set1_rect.top + 230))
                    screen.blit(v_set, (image_bg_set1_rect.left + 190, image_bg_set1_rect.top + 50))
                    # 绘制音量线
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (line_left, image_bg_set1_rect.top + 155,),
                                     (line_right, image_bg_set1_rect.top + 155),
                                     6)
                    pygame.draw.line(screen, COLOR_BLACK,
                                     (line_left, image_bg_set1_rect.top + 255,),
                                     (line_right, image_bg_set1_rect.top + 255),
                                     6)
                    pygame.draw.line(screen, COLOR_GREEN,
                                     (line_left + 2, image_bg_set1_rect.top + 255,),
                                     (button_volume2_rect.left, image_bg_set1_rect.top + 255),
                                     3)
                    pygame.draw.line(screen, COLOR_GREEN,
                                     (line_left + 2, image_bg_set1_rect.top + 155,),
                                     (button_volume1_rect.left, image_bg_set1_rect.top + 155),
                                     3)

                    # 绘制音量滑轮按钮

                    screen.blit(button_volume1, button_volume1_rect)
                    screen.blit(button_volume2, button_volume2_rect)
                else:
                    screen.blit(background_login, (0, 0))
                    screen.blit(start_but, start_but_rect);
                    screen.blit(rank_but, rank_but_rect)
                    screen.blit(logout_but, logout_but_rect)
                    screen.blit(show_way, show_way_rect)
                    screen.blit(volume1_button_image, volume1_button_rect)
                    screen.blit(button_end, button_end_rect)
                pygame.display.update()
                for event in pygame.event.get():
                    if event.type == QUIT:
                        # pygame.quit()
                        # sys.exit()
                        return EXIT_PAGE
                    if pygame.mouse.get_pressed()[0] and not choice:
                        pos = pygame.mouse.get_pos()
                        #点击开始游戏按钮
                        if start_but_rect.left < pos[0] < start_but_rect.right and \
                                start_but_rect.top < pos[1] < start_but_rect.bottom and not paint_set:
                            SoundControlUtils.sound_button.play()
                            # score = start(choose_plane())
                            return CHOOSE_PLANE_PAGE
                            # if score >= 0:
                            #     choice = end_paint(user_id, score)
                            #     button_volume1_rect.left, button_volume1_rect.top = volume_bg_pos, 339
                            #     button_volume2_rect.left, button_volume2_rect.top = volume_effect_pos, 439
                            # else:
                            #     choice = 0
                        #点击排行版按钮
                        elif rank_but_rect.left < pos[0] < rank_but_rect.right and \
                                rank_but_rect.top < pos[1] < rank_but_rect.bottom and not paint_set:
                            SoundControlUtils.sound_button.play()
                            SoundControlUtils.sound_bgm1.stop()
                            return RANK_PAGE
                        #点击注销登录按钮
                        elif logout_but_rect.left < pos[0] < logout_but_rect.right and \
                                logout_but_rect.top < pos[1] < logout_but_rect.bottom and not paint_set:
                            SoundControlUtils.sound_button.play()
                            # user = reg_login2()
                            return LOGIN_PAGE
                        #点击退出游戏按钮
                        elif button_end_rect.left < event.pos[0] < button_end_rect.right and \
                                button_end_rect.top < event.pos[1] < button_end_rect.bottom:
                            SoundControlUtils.sound_button.play()
                            return EXIT_PAGE
                        #点击教程按钮
                        elif show_way_rect.left < pos[0] < show_way_rect.right and \
                                show_way_rect.top < pos[1] < show_way_rect.bottom:
                            SoundControlUtils.sound_button.play()
                            return OPERATING_INSTRUCTIONS_PAGE
                        elif image_bg_set2_rect.left < pos[0] < image_bg_set2_rect.right and \
                                image_bg_set2_rect.top < pos[1] < image_bg_set2_rect.bottom and paint_set:
                            paint_set = False
                            H_i = (
                                          height - image_bg_set1_rect.bottom + image_bg_set1_rect.top) / 2 + image_bg_set1_rect.bottom
                            H_j = 20
                        # 打开音量控制
                        elif volume1_button_rect.left < pos[0] < volume1_button_rect.right and \
                                volume1_button_rect.top < pos[1] < volume1_button_rect.bottom and not paint_set:
                            paint_set = True
                    if event.type == MOUSEMOTION and not choice:
                        pos = pygame.mouse.get_pos()

                        # 音量键滑动控制
                        if button_volume1_rect.left < pos[0] < button_volume1_rect.right and \
                                button_volume1_rect.top - 30 < pos[1] < button_volume1_rect.bottom + 30 and \
                                paint_set and line_left + 19 < pos[0] < line_right - 20:
                            button_volume1_rect.left = pos[0] - 20
                            H_t = (button_volume1_rect.left - line_left) / (line_right - line_left)
                            #更改bgm音量大小
                            SoundControlUtils.change_bgm_volume(H_t)

                        #音效滑动控制
                        elif button_volume2_rect.left < pos[0] < button_volume2_rect.right and \
                                button_volume2_rect.top - 30 < pos[1] < button_volume2_rect.bottom + 30 and \
                                paint_set and line_left + 19 < pos[0] < line_right - 20:
                            button_volume2_rect.left = pos[0] - 20

                            #计算音效大小
                            H_t = (button_volume2_rect.left - line_left) / (line_right - line_left)
                            #更改音效音量大小
                            SoundControlUtils.change_soundEffect_volume(H_t)
                            #试音
                            SoundControlUtils.sound_button.play()

    def write_score(user_id, score):
        f = open('data/rank.txt')
        user = []
        rank = []
        for i in range(10):
            user.append(f.readline()[:-1])
            rank.append(int(f.readline()[:-1]))
        f.close()
        i = 0
        for s in rank :
            if score > s:
                rank.insert(i,score)
                user.insert(i,user_id)
                break
            i+=1
        w = open('data/rank.txt','w')
        for i in range(10):
            w.write(user[i] + "\n")
            w.write(str(rank[i]) + "\n")
        w.close()